/*
 *  Copyright (C) 2010-2021 Fabio Cavallo (aka FHorse)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "mappers.h"
#include "info.h"
#include "mem_map.h"

void map_init_BMCHP898F(void) {
	EXTCL_CPU_WR_MEM(BMCHP898F);

	extcl_cpu_wr_mem_BMCHP898F(0x6004, 0);

	info.mapper.extend_wr = TRUE;
}
void extcl_cpu_wr_mem_BMCHP898F(WORD address, BYTE value) {
	if ((address & 0x6000) == 0x6000) {
		if (address & 0x0004) {
			BYTE base = (value & 0x38) >> 3;
			BYTE high = (value & 0x40) >> 4;

			if (value & 0x80) {
				mirroring_V();
			} else {
				mirroring_H();
			}

			value = (~high) & base;
			control_bank(info.prg.rom[0].max.banks_16k)
			map_prg_rom_8k(2, 0, value);
			value = high | base;
			control_bank(info.prg.rom[0].max.banks_16k)
			map_prg_rom_8k(2, 2, value);
			map_prg_rom_8k_update();
			return;
		} else {
			DBWORD bank;

			value = ((value & 0x70) >> 4) & (~(((value & 0x01) << 2) | (value & 0x02)));
			control_bank(info.chr.rom[0].max.banks_8k)
			bank = value << 13;
			chr.bank_1k[0] = chr_chip_byte_pnt(0, bank);
			chr.bank_1k[1] = chr_chip_byte_pnt(0, bank | 0x0400);
			chr.bank_1k[2] = chr_chip_byte_pnt(0, bank | 0x0800);
			chr.bank_1k[3] = chr_chip_byte_pnt(0, bank | 0x0C00);
			chr.bank_1k[4] = chr_chip_byte_pnt(0, bank | 0x1000);
			chr.bank_1k[5] = chr_chip_byte_pnt(0, bank | 0x1400);
			chr.bank_1k[6] = chr_chip_byte_pnt(0, bank | 0x1800);
			chr.bank_1k[7] = chr_chip_byte_pnt(0, bank | 0x1C00);
			return;
		}
	}
}
